home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / TinyGL / ami / content / ad709 / tinygl / src / texture.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-15  |  3.9 KB  |  216 lines

  1. /*$T texture.c GC 1.137 08/09/02 17:47:18 */
  2.  
  3. /* Texture Manager */
  4. #include "texture.h"
  5.  
  6. /* */
  7.  
  8. GLTexture *find_texture(GLContext *c, int h) {
  9.     GLTexture    *t;
  10.     /*~~~~~~~~~~~*/
  11.  
  12.     t = c->shared_state.texture_hash_table[h % TEXTURE_HASH_TABLE_SIZE];
  13.     while(t != NULL) {
  14.         if(t->handle == h) {
  15.             return t;
  16.         }
  17.  
  18.         t = t->next;
  19.     }
  20.  
  21.     return NULL;
  22. }
  23.  
  24. /* */
  25. void free_texture(GLContext *c, int h) {
  26.     GLTexture    *t, **ht;
  27.     GLImage        *im;
  28.     int            i;
  29.  
  30.     t = find_texture(c, h);
  31.     if(t->prev == NULL) {
  32.         ht = &c->shared_state.texture_hash_table[t->handle % TEXTURE_HASH_TABLE_SIZE];
  33.         *ht = t->next;
  34.     }
  35.     else {
  36.         t->prev->next = t->next;
  37.     }
  38.  
  39.     if(t->next != NULL) {
  40.         t->next->prev = t->prev;
  41.     }
  42.  
  43.     for(i = 0; i < MAX_TEXTURE_LEVELS; i++) {
  44.         im = &t->images[i];
  45.         if(im->pixmap != NULL) {
  46.             free(im->pixmap);
  47.         }
  48.     }
  49.  
  50.     free(t);
  51. }
  52.  
  53. /* */
  54. GLTexture *alloc_texture(GLContext *c, int h) {
  55.     GLTexture    *t, **ht;
  56.  
  57.     t = calloc(1, sizeof(GLTexture));
  58.  
  59.     ht = &c->shared_state.texture_hash_table[h % TEXTURE_HASH_TABLE_SIZE];
  60.  
  61.     t->next = *ht;
  62.     t->prev = NULL;
  63.     if(t->next != NULL) {
  64.         t->next->prev = t;
  65.     }
  66.  
  67.     *ht = t;
  68.  
  69.     t->handle = h;
  70.  
  71.     return t;
  72. }
  73.  
  74. /* */
  75. void glInitTextures(GLContext *c) {
  76.     /* textures */
  77.     c->texture_2d_enabled = 0;
  78.     c->current_texture = find_texture(c, 0);
  79. }
  80.  
  81. /* */
  82. void glopBindTexture(GLContext *c, GLParam *p) {
  83.     int            target = p[1].i;
  84.     int            texture = p[2].i;
  85.     GLTexture    *t;
  86.  
  87.     assert(target == GL_TEXTURE_2D && texture >= 0);
  88.  
  89.     t = find_texture(c, texture);
  90.     if(t == NULL) {
  91.         t = alloc_texture(c, texture);
  92.     }
  93.  
  94.     c->current_texture = t;
  95. }
  96.  
  97. /* */
  98. void glopTexImage2D(GLContext *c, GLParam *p) {
  99.     int                target = p[1].i;
  100.     int                level = p[2].i;
  101.     int                components = p[3].i;
  102.     int                width = p[4].i;
  103.     int                height = p[5].i;
  104.     int                border = p[6].i;
  105.     int                format = p[7].i;
  106.     int                type = p[8].i;
  107.     void            *pixels = p[9].p;
  108.     GLImage            *im;
  109.     unsigned char    *pixels1;
  110.     int                do_free;
  111.  
  112.     if(!(target == GL_TEXTURE_2D && level == 0 && components == 3 && border == 0 && format == GL_RGB &&
  113.            type == GL_UNSIGNED_BYTE)) {
  114.         gl_fatal_error("glTexImage2D: combinaison of parameters not handled");
  115.     }
  116.  
  117.     do_free = 0;
  118.     if(width != 256 || height != 256) {
  119.         pixels1 = malloc(256 * 256 * 3);
  120.  
  121.         /* no interpolation is done here to respect the original image aliasing ! */
  122.         gl_resizeImageNoInterpolate(pixels1, 256, 256, pixels, width, height);
  123.         do_free = 1;
  124.         width = 256;
  125.         height = 256;
  126.     }
  127.     else {
  128.         pixels1 = pixels;
  129.     }
  130.  
  131.     im = &c->current_texture->images[level];
  132.     im->xsize = width;
  133.     im->ysize = height;
  134.     if(im->pixmap != NULL) {
  135.         free(im->pixmap);
  136.     }
  137.  
  138. #if TGL_FEATURE_RENDER_BITS == 24
  139.     im->pixmap = malloc(width * height * 3);
  140.     if(im->pixmap) {
  141.         memcpy(im->pixmap, pixels1, width * height * 3);
  142.     }
  143.  
  144. #elif TGL_FEATURE_RENDER_BITS == 32
  145.     im->pixmap = malloc(width * height * 4);
  146.     if(im->pixmap) {
  147.         gl_convertRGB_to_8A8R8G8B(im->pixmap, pixels1, width, height);
  148.     }
  149.  
  150. #elif TGL_FEATURE_RENDER_BITS == 16
  151.     im->pixmap = malloc(width * height * 2);
  152.     if(im->pixmap) {
  153.         gl_convertRGB_to_5R6G5B(im->pixmap, pixels1, width, height);
  154.     }
  155.  
  156. #else
  157.     #error TODO
  158. #endif
  159.     if(do_free) {
  160.         free(pixels1);
  161.     }
  162. }
  163.  
  164. /* TODO: not all tests are done */
  165. void glopTexEnv(GLContext *c, GLParam *p) {
  166.     int target = p[1].i;
  167.     int pname = p[2].i;
  168.     int param = p[3].i;
  169.  
  170.     if(target != GL_TEXTURE_ENV)
  171.     {
  172. error:
  173.         gl_fatal_error("glTexParameter: unsupported option");
  174.     }
  175.  
  176.     if(pname != GL_TEXTURE_ENV_MODE) {
  177.         goto error;
  178.     }
  179.  
  180.     if(param != GL_DECAL) {
  181.         goto error;
  182.     }
  183. }
  184.  
  185. /* TODO: not all tests are done */
  186. void glopTexParameter(GLContext *c, GLParam *p) {
  187.     int target = p[1].i;
  188.     int pname = p[2].i;
  189.     int param = p[3].i;
  190.  
  191.     if(target != GL_TEXTURE_2D)
  192.     {
  193. error:
  194.         gl_fatal_error("glTexParameter: unsupported option");
  195.     }
  196.  
  197.     switch(pname) {
  198.     case GL_TEXTURE_WRAP_S:
  199.     case GL_TEXTURE_WRAP_T:
  200.         if(param != GL_REPEAT) {
  201.             goto error;
  202.         }
  203.         break;
  204.     }
  205. }
  206.  
  207. /* */
  208. void glopPixelStore(GLContext *c, GLParam *p) {
  209.     int pname = p[1].i;
  210.     int param = p[2].i;
  211.  
  212.     if(pname != GL_UNPACK_ALIGNMENT || param != 1) {
  213.         gl_fatal_error("glPixelStore: unsupported option");
  214.     }
  215. }
  216.